/**
 * 适配 pagination数据源
 * @author xiufu.wang
 */
import debounce from 'throttle-debounce/debounce';

export default function applyAdapt(dataSource) {
    dataSource['size-change'] = debounce(200, (pageSize) => {
        const opt = dataSource.mergeOption
        const pageSizeField = opt.pageSizeField || 'pageSize'
        const currentPageField = opt.currentPageField || 'current'
        //请求分页数据
        dataSource.store.pagination = {
            ...dataSource.pagination,
            pageSize: pageSize,
            currentPage: 1
        }

        dataSource.disableDefaultPageSize = true

        dataSource.shortLoadDatas({
            [pageSizeField]: pageSize,
            [currentPageField]: dataSource.store.pagination.currentPage
        })
        let vm = dataSource.getScope();
        let tableInstance = (vm.$children || []).find(v => v.$options._componentTag === 'pro-data-table');
        tableInstance && tableInstance.$emit('expose-current-change','size-change')
    })

    dataSource['current-change'] = debounce(200, (current) => {
         const opt = dataSource.mergeOption
        const pageSizeField = opt.pageSizeField || 'pageSize'
        const currentPageField = opt.currentPageField || 'current'

        //请求分页数据
        dataSource.store.pagination = {
            ...dataSource.pagination,
            currentPage: current
        }
        dataSource.shortLoadDatas({
            [pageSizeField]: dataSource.store.pagination.pageSize,
            [currentPageField]: current
        })
        let vm = dataSource.getScope();
        let tableInstance = (vm.$children || []).find(v => v.$options._componentTag === 'pro-data-table');
        tableInstance && tableInstance.$emit('expose-current-change','current-change')
    })
}